<template>
  <!-- 添加或修改厂内编码对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-tabs v-model="activeName" class="detail-tab" type="card">
        <el-tab-pane label="基本信息" name="first">
          <el-row :gutter="20">
            <el-col :span="24" v-if="goodsSelect">
              <el-form-item label="选择产品" prop="goodsId">
                <el-select v-model="form.goodsId" placeholder="请选择产品" style="width: 100%;"
                           remote reserve-keyword :remote-method="searchGoods" @change="selectGoods" filterable
                           clearable>
                  <el-option v-for="item in goodsList"
                             :key="item.id" :label="item.label" :value="item.id"/>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="内部名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入名称"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="内部编号" prop="code">
                <el-input v-model="form.code" placeholder="保存后自动生成" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="内部型号" prop="model">
                <el-input v-model="form.model" placeholder="请输入型号"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input type="textarea" v-model="form.remark" placeholder="请输入备注"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {addAction, getAction, getById, updateAction} from '@/api/manage';
import moment from 'moment';

export default {
  name: "GoodsOrderInnerForm",
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        goodsId: [
          {required: true, message: "请选择产品", trigger: "change"}
        ],
        name: [
          {required: true, message: "请输入名称", trigger: "change"}
        ],
        model: [
          {required: true, message: "请输入型号", trigger: "change"}
        ],
      },
      goodsList: [],
      goodsSelect: true,
      url: {
        getById: '/goodsOrderInner/goodsOrderInner/',
        add: '/goodsOrderInner/goodsOrderInner/add',
        edit: '/goodsOrderInner/goodsOrderInner/edit',
      },
    };
  },
  created() {

  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goodsId: null,
        name: null,
        code: null,
        model: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加厂内编码";
    },

    /** 新增按钮操作 */
    handleAddByGoods(goods) {
      this.goodsSelect = false;
      this.reset();
      this.open = true;
      this.form.goodsId= goods.id;
      this.title = "添加厂内编码";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getById(this.url.getById, id).then(response => {
        this.form = response.data;
        this.searchGoods(response.data.params.name);
        this.open = true;
        this.title = "修改厂内编码";
      });
    },

    searchGoods(query) {
      if (query !== '') {
        this.loading = true; // 开始加载数据
        let params = {"queryLabel": query, "pageNum": 1, "pageSize": 50,};
        getAction('/goodsOrderIn/goodsOrderIn/list', params).then(response => {
          this.goodsList = response.rows;
          for (let i = 0; i < this.goodsList.length; i++) {
            this.goodsList[i].label = this.goodsList[i].name + '-' + this.goodsList[i].model + '-' + this.goodsList[i].code;
          }
        })
      } else {
        this.goodsList = [];
      }
    },

    selectGoods(value) {
      for (let i = 0; i < this.goodsList.length; i++) {
        if (this.goodsList[i].id == value) {
          this.form.goodsId = this.goodsList[i].id;
          this.$forceUpdate();
        }
      }
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAction(this.url.edit, this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit('ok');
            });
          } else {
            addAction(this.url.add, this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit('ok');
            });
          }
        }
      });
    },


  }
};
</script>
